<?php

namespace App\Http\Controllers\Admin;

use App\Models\Photos;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PhotosController extends Controller
{
    public function index(Request $request)
    {
        $limit = $request->get('limit', 10);
        $category_id = $request->get('category_id', -1);
        $model = Photos::query();
        if ($category_id != -1) {
            $model = $model->where('category_id', $category_id);
        }
        $result = $model->orderBy('id', 'desc')->paginate($limit);
        $ary['count'] = $result->total();
        $ary['limit'] = $limit;
        $ary['list'] = $result->items();
        return showMsg('获取成功', 200, $ary);
    }

    public function store(Request $request)
    {
        $data = $request->only(['title', 'sort']);
        Photos::create($data);
        return showMsg('添加成功', 200);
    }

    public function edit(Request $request)
    {
        $id = $request->get('id');
        $re = Photos::findOrFail($id);
        return showMsg('获取成功', 200, $re);
    }

    public function update(Request $request)
    {
        $id = $request->get('id');
        $slide = Photos::findOrFail($id);
        $data = $request->only(['title', 'sort']);
        $slide->update($data);
        return showMsg('更新成功', 200);
    }

    public function destroy(Request $request)
    {
        $id = $request->get('id');
        $ids = $request->get('ids');
        if (count($ids) > 0) {
            foreach ($ids as $v) {
                //删除实际图片地址
                try {
                    $path = Photos::where('id', $v)->value('photo');
                    if ($path) {
                        $pos = strpos($path, 'uploads');
                        /*dd($pos);*/
                        $path = substr($path, $pos, strlen($path));
                        //dd($path);
                        unlink("$path");
                    }
                } catch (\Exception $exception) {

                }
                Photos::destroy('id', $v);
            }
        } else {
            //删除实际图片地址
            $path = Photos::where('id', $id)->value('photo');
            if ($path) {
                try {
                    $pos = strpos($path, 'uploads');
                    /*dd($pos);*/
                    $path = substr($path, $pos, strlen($path));
                    //dd($path);
                    unlink("$path");
                } catch (\Exception $exception) {

                }
            }
            Photos::destroy('id', $id);
        }
        return showMsg('删除成功', 200);
    }

    public function editCat(Request $request)
    {
        $cat_id = $request->get('cat_id');
        $id = $request->get('id');
        $ids = $request->get('ids');
        if ($ids) {
            foreach ($ids as $v) {
                Photos::where('id', $v)->update(['category_id' => $cat_id]);
            }
        } else {
            Photos::where('id', $id)->update(['category_id' => $cat_id]);
        }
        return showMsg('操作成功', 200);
    }
}
